import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:zootix/zt_app_theme.dart';

class ZtSnackBarUtil {
  checkIsSnackBarInit() {
    return Get.overlayContext != null;
  }

  Future<void> error(String? title, {dynamic error}) async {
    if (checkIsSnackBarInit()) {
      if (error is Exception) {
        title = error.toString();
      }

      if (error is DioException) {
        title = error.message;
        if (error.response?.data is Map) {
          title = error.response?.data['message'] ?? title;
        }
      }
      if (Get.isSnackbarOpen) {
        await Get.closeCurrentSnackbar();
      }
      Get.rawSnackbar(
          message: title,
          snackPosition: SnackPosition.TOP,
          backgroundColor: ZtAppTheme.snackbarErrorBackgroudColor,
          borderRadius: 15,
          margin: EdgeInsets.symmetric(horizontal: 10, vertical: 0),
          overlayColor: Colors.white,
          duration: Duration(seconds: 3),
          forwardAnimationCurve: Curves.fastLinearToSlowEaseIn,
          reverseAnimationCurve: Curves.linearToEaseOut);
    }
  }

  Future<void> success(String title, {String? message}) async {
    if (checkIsSnackBarInit()) {
      if (Get.isSnackbarOpen) {
        await Get.closeCurrentSnackbar();
      }
      Get.rawSnackbar(
          message: title,
          snackPosition: SnackPosition.TOP,
          backgroundColor: ZtAppTheme.snackbarSuccessBackgroudColor,
          borderRadius: 15,
          margin: EdgeInsets.symmetric(horizontal: 10, vertical: 0),
          overlayColor: Colors.white,
          duration: Duration(seconds: 2),
          dismissDirection: DismissDirection.horizontal,
          forwardAnimationCurve: Curves.fastLinearToSlowEaseIn,
          reverseAnimationCurve: Curves.linearToEaseOut);
    }
  }

  Future<void> info(String title, {String? message}) async {
    if (checkIsSnackBarInit()) {
      if (Get.isSnackbarOpen) {
        await Get.closeCurrentSnackbar();
      }
      Get.rawSnackbar(
          message: title,
          snackPosition: SnackPosition.TOP,
          borderRadius: 15,
          margin: EdgeInsets.symmetric(horizontal: 10, vertical: 0),
          duration: Duration(seconds: 2),
          dismissDirection: DismissDirection.horizontal,
          forwardAnimationCurve: Curves.fastLinearToSlowEaseIn,
          reverseAnimationCurve: Curves.linearToEaseOut);
    }
  }

  Future<void> warning(String title, {String? message}) async {
    if (checkIsSnackBarInit()) {
      if (Get.isSnackbarOpen) {
        await Get.closeCurrentSnackbar();
      }
      Get.rawSnackbar(
          message: title,
          snackPosition: SnackPosition.TOP,
          backgroundColor: ZtAppTheme.snackbarWarningBackgroudColor,
          borderRadius: 15,
          margin: EdgeInsets.symmetric(horizontal: 10, vertical: 0),
          overlayColor: Colors.white,
          duration: Duration(seconds: 2),
          dismissDirection: DismissDirection.horizontal,
          forwardAnimationCurve: Curves.fastLinearToSlowEaseIn,
          reverseAnimationCurve: Curves.linearToEaseOut);
    }
  }
}
